package com.itheima.factory;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class BeanFactory {
    public static final Map<String,Object> cache = new HashMap<>();
    static {
        Properties properties = new Properties();
        InputStream is = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Set<String> names = properties.stringPropertyNames();
        names.forEach(name->{
            String className = properties.getProperty(name);
            Object bean = null;
            try {
                bean = Class.forName(className).newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
            cache.put(name,bean);
        });
    }
    public static<T> T getBean(String name,Class<T> tClass){
        return (T) cache.get(name);
    }
}
